package org.kernely.security.dao;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.kernely.persistence.service.SessionService;
import org.kernely.security.persistence.Permission;
import org.osgi.service.component.ComponentContext;

public class PermissionDAO {
	/**
	 * SessionService service
	 */
	private static SessionService service;

	/**
	 * The group dao instance
	 */
	private static PermissionDAO instance;

	/**
	 * Store the group dao instance 
	 * @return the group dao
	 */
	public static PermissionDAO getInstance() {
		return instance;
	}

	/**
	 * Set the persistence session service
	 * @param sessionService the session service to set
	 */
	public final void setPersistenceService(SessionService sessionService) {
		service = sessionService;
	}

	/**
	 * Activate the component, called by SC runtime
	 * @param ctxt the activation context
	 */
	protected final void activate(ComponentContext ctxt) {
		instance = this;
	}

	/**
	 * Gets the session service.
	 * 
	 * @return the session service
	 */
	public static SessionService getService() {
		return service;
	}

	/**
	 * Sets the session service.
	 * 
	 * @param service
	 *            the session service to set
	 */
	public static void setService(SessionService service) {
		PermissionDAO.service = service;
	}

	/**
	 * Returns all the Permissions stored
	 * @return a list of all Permissions stored
	 */
	public final Set<Permission> getAllPermissions(){
		Session session = service.getSession();
		List<Permission> list = (List<Permission>) session.createCriteria(Permission.class).list();
		return new HashSet<Permission>(list);
	}

	/**
	 * Returns the Permission which have this id
	 * @param id : the id of the needed Permission
	 * @return the corresponding role
	 */
	public final Permission getPermissionById(int id){
		Session session = service.getSession();
		Criteria crit = session.createCriteria(Permission.class);
		crit.add(Restrictions.like("id", id));
		return (Permission) crit.list().get(0);
	}

	/**
	 * Returns the Permission which have this name
	 * @param name : this name of the needed Permission
	 * @return : the corresponding Permission
	 */
	public final Permission findPermissionByName(String name){
		Session session = service.getSession();
		Criteria crit = session.createCriteria(Permission.class);
		crit.add(Restrictions.like("name", name));
		List<Permission> list = crit.list();
		if (list.size()<=0){
			return null;
		}
		return (Permission) list.get(0);
	}

	/**
	 * Persist the new Permission
	 * @param r : the new Permission
	 */
	public final void savePermission(Permission p){
		Session session = service.getSession();
		session.save(p);
	}

	/**
	 * Update an existing Permission
	 * @param r : the Permission to update
	 */
	public final void updatePermission(Permission p){
		Session session = service.getSession();
		session.merge(p);
	}

	/**
	 * Find a permission by the resource id.
	 * @param resourceID
	 * @return all the permissions matching with the resource id
	 */
	public final Set<Permission> findPermissionByResourceID(String resourceID){
		Session session = service.getSession();
		Criteria crit = session.createCriteria(Permission.class);
		crit.add(Restrictions.like("name", resourceID, MatchMode.END));
		List<Permission> list = crit.list();
		if (list.size()<=0){
			return null;
		}
		return new HashSet<Permission>(list);
	}
}
